﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;

namespace script.UI
{
    public class DragItem : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
    {
        private ItemUI currentItemUI;
        private SlotHolder currentHolder;
        private SlotHolder targetHolder;

        private void Awake()
        {
            currentItemUI = GetComponent<ItemUI>();
            currentHolder = GetComponentInParent<SlotHolder>();
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (currentHolder.slotType != SlotType.SHOP)
            {
                InventoryManager.Instance.currentDragData = new InventoryManager.DragData();
                InventoryManager.Instance.currentDragData.originalHolder = GetComponentInParent<SlotHolder>();
                InventoryManager.Instance.currentDragData.originalParent = (RectTransform) transform.parent;
            }

            //记录初始的数据
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (currentHolder.slotType != SlotType.SHOP)
            {
                //跟随鼠标的位置
                transform.position = eventData.position;
                transform.SetParent(InventoryManager.Instance.dragCanvas.transform, true);
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            //放下物品，交换数据
            //是否指向UI
            if (EventSystem.current.IsPointerOverGameObject())
            {
                if (InventoryManager.Instance.CheakBagUI(eventData.position) ||
                    InventoryManager.Instance.CheakActionUI(eventData.position) ||
                    InventoryManager.Instance.CheakEquipUI(eventData.position))
                {
                    if (eventData.pointerEnter.gameObject.GetComponent<SlotHolder>())
                    {
                        targetHolder = eventData.pointerEnter.gameObject.GetComponent<SlotHolder>();
                    }
                    else
                    {
                        targetHolder = eventData.pointerEnter.gameObject.GetComponentInParent<SlotHolder>();
                    }

                    if (targetHolder != InventoryManager.Instance.currentDragData.originalHolder)
                        switch (targetHolder.slotType)
                        {
                            case SlotType.BAG:
                                SwapItem();
                                break;
                            case SlotType.ACTION:
                                if (currentItemUI.Bag.items[currentItemUI.Index].itemData.itemType == ItemType.Useable)
                                    SwapItem();
                                break;
                            case SlotType.WEAPON:
                                if (currentItemUI.Bag.items[currentItemUI.Index].itemData.itemType == ItemType.Weapon)
                                    SwapItem();
                                break;
                            case SlotType.ARMOR:
                                if (currentItemUI.Bag.items[currentItemUI.Index].itemData.itemType == ItemType.Armor)
                                    SwapItem();
                                break;
                        }

                        currentHolder.updateItem();
                        targetHolder.updateItem();
                }
                
            }

            if (currentHolder.slotType != SlotType.SHOP)
            {
                transform.SetParent(InventoryManager.Instance.currentDragData.originalParent, true);
                RectTransform t = (RectTransform) transform;
                t.offsetMax = -Vector2.one * 5;
                t.offsetMin = Vector2.one * 5;
            }
        }

        public void SwapItem()
        {
            var targetItem = targetHolder.itemUI.Bag.items[targetHolder.itemUI.Index];
            var tempItem = currentHolder.itemUI.Bag.items[currentHolder.itemUI.Index];

            bool isSameItem = targetItem.itemData == tempItem.itemData;
            if (isSameItem && targetItem.itemData.stackable)
            {
                targetItem.amount += tempItem.amount;
                tempItem.itemData = null;
                tempItem.amount = 0;
            }
            else
            {
                currentHolder.itemUI.Bag.items[currentHolder.itemUI.Index] = targetItem;
                targetHolder.itemUI.Bag.items[targetHolder.itemUI.Index] = tempItem;
            }
        }
    }
   
}